<template>
  <div class="wrapper">
    <div class="title-wrapper">
      <div class="bigTitle">车辆利润统计</div>
      <div style="display:flex;text-align: right">
        <time-picker ref="timeRef" @change="handleChange" />
        <el-radio-group size="mini" v-model="seriesName" style="margin-left: 10px" @change="handleRadio">
          <el-radio-button label="运费"></el-radio-button>
          <el-radio-button label="利润"></el-radio-button>
          <el-radio-button label="油费"></el-radio-button>
          <el-radio-button label="里程"></el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <vehicle-bar :seriesName="seriesName" :source="manData" />
  </div>
</template>

<script>
import {carSeatsAnalysis} from "@/api/services/dashboard.service";
import timePicker from './time-picker'
import vehicleBar from './vehicle-bar'
import moment from "moment";

const state = {
  '运费':0,
  '利润':1,
  '油费':2,
  '里程':3,
}
export default {
  name: "vehicle",
  components: {vehicleBar,timePicker},
  data() {
    return {
      manData: {
        seats:[],
        maxPrice:[],
        avgResult:[],
        plate:[]
      },
      currentTime:'',
      seriesName: '运费',
      params: {
        countType: 0,
        year: '',
        month: ''
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      let time = this.$refs.timeRef.initTime()
      this.handleChange(time)
    })
  },
  methods: {
    handleRadio(data) {
      this.seriesName = data
      this.getData()
    },
    handleChange(data) {
      if (!data) return
      this.currentTime = `${data.year}-${data.moment}`
      this.getData()
    },
    getData() {
      let currentTime = this.currentTime
      let params = {stime: '', etime: '',type:state[this.seriesName]}
      let month = moment(this.currentTime, "YYYY-MM").daysInMonth()
      if (currentTime) {
        params.stime = `${currentTime}-01 00:00:00`
        params.etime = `${currentTime}-${month} 23:59:59`
      }
      carSeatsAnalysis(params).then(res => {
        let seats = []
        let maxPrice = []  // 最高金额
        let avgResult = []  //平均值
        let plate = []
        res.data.forEach(item => {
          seats.push(item.seats)
          plate.push(item.maxPlate)
          maxPrice.push(item.maxPrice)
          avgResult.push(item.avgResult)
        })
        this.manData = {
          seats,maxPrice,avgResult, plate
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.title-wrapper {
  display: flex;
  justify-content: space-between;

  .bigTitle {
    border-bottom: none;
    padding: 8px 0;
    color: #3A71A8;
    font-size: 18px;
    font-weight: bold;
    white-space: nowrap;
  }
}
.total_wrapper {
  width: 80%;
  margin: 0 auto;
  text-align: center;
  .item {

  }
}

.wrapper {
  width: 100%;
  height: 300px;
}
</style>
